package com.ts.api.module.match.plan.internal.qualification.handler;

import com.gitee.apanlh.exp.ParamParserException;
import com.ts.api.module.match.plan.internal.core.PlanMatchChannelType;
import com.ts.api.module.match.plan.internal.qualification.context.QualificationContext;
import com.ts.api.module.match.plan.internal.qualification.model.PlanQualification;
import com.ts.api.module.match.plan.internal.qualification.model.PlanQualificationResult;
import com.ts.api.module.match.plan.internal.qualification.strategy.PlanQualificationStrategy;
import com.ts.api.module.match.plan.model.AbstractPlanMatch;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 *  计划资质处理器
 *
 *  @author Pan
 */
@Component
public class PlanQualificationHandler {

    @Autowired
    private QualificationContext planQualificationContext;

    /**
     *  过滤用户资质与计划资质，返回符合条件的计划列表
     *
     *  @param  abstractPlanMatch    抽象实体
     *  @return PlanQualificationResult  资质验证返回实体类
     */
    public PlanQualificationResult handleFilter(AbstractPlanMatch abstractPlanMatch) {
        PlanMatchChannelType planMatchChannelType = abstractPlanMatch.getPlanMatchChannelType();
        PlanQualificationStrategy context = planQualificationContext.getContext(planMatchChannelType);

        //  转化资质校验实体
        PlanQualification planQualification;
        try {
            planQualification = context.convert(abstractPlanMatch);
        } catch (Exception e) {
            return PlanQualificationResult.ofFail(new ParamParserException("转换资质类型-失败:" + e.getMessage(), e));
        }

        //  资质过滤
        try {
            return context.filter(planQualification, abstractPlanMatch);
        } catch (Exception e) {
            return PlanQualificationResult.ofFail(new ParamParserException("过滤资质-失败:" + e.getMessage(), e));
        }
    }
}
